//
//  SearchView.m
//  XiXi
//
//  Created by HawkingWang on 16/9/29.
//  Copyright © 2016年 xi-xi.cc. All rights reserved.
//

#import "SearchView.h"

#import "OnlineTableViewCell.h"//cell

@implementation SearchView

 static NSString *const OnlineTableViewCellID = @"OnlineTableViewCell";

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSearchView];
        
        UIColor *color1 = RGBACOLOR(34, 36, 44, 1);
        UIColor *color2 = RGBACOLOR(34, 36, 45, 0.5);
        CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
        gradientLayer.colors = @[(__bridge id)color1.CGColor,(__bridge id)color2.CGColor];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1);
        gradientLayer.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        [self.layer insertSublayer:gradientLayer atIndex:0];

        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
        effectView.frame = CGRectMake( 0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        [self addSubview:effectView];
    }
    return self;
}

- (void)createSearchView{
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    blackView.backgroundColor = RGBCOLOR(34, 36, 45);
    [self addSubview:blackView];
    
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(15, 28, SCREEN_WIDTH - 15 - 65, 28)];
    searchView.backgroundColor = [UIColor whiteColor];
    searchView.layer.cornerRadius = 15;
    searchView.layer.masksToBounds = YES;
    [blackView addSubview:searchView];
    
    _searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(8, 0, searchView.frame.size.width - 16, searchView.frame.size.height)];
    _searchTextField.placeholder = @"找到所要查看的在线剧目";
    _searchTextField.textAlignment = NSTextAlignmentCenter;
    _searchTextField.font = [UIFont fontWithName:@"PingFang SC" size:12];
    _searchTextField.textColor = RGBCOLOR(149, 145, 167);
    _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _searchTextField.returnKeyType = UIReturnKeySearch;
    _searchTextField.keyboardAppearance = UIKeyboardAppearanceDark;
    _searchTextField.delegate = self;
    [searchView addSubview:_searchTextField];
    
    _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _searchBtn.frame = CGRectMake(SCREEN_WIDTH - 46, 30, 30.5, 21);
    [_searchBtn setTitle:@"取消" forState:UIControlStateNormal];
    _searchBtn.backgroundColor = RGBCOLOR(34, 36, 45);
    [_searchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _searchBtn.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size:15];
    [self addSubview:_searchBtn];
}

#pragma mark === TextFieldDelegate ===
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    DLog(@"搜索%@", _searchTextField.text);
    
    
    if (!_searchTableView) {
        [self createTableView];
    }else{
        [_searchTableView reloadData];
    }
    

    return YES;
}

#pragma mark === TableViewDelegate ===
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OnlineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OnlineTableViewCellID];
    cell.image.backgroundColor = [UIColor colorWithRed:VALUE green:VALUE blue:VALUE alpha:1];
    cell.title.text = @"阿加莎推理巨作——爱乐汇·拉得斯基进行曲——世界";
    cell.title.textColor = [UIColor whiteColor];
    cell.price.text = @"120元 - 360元";
    cell.time.text = @"2016.01.10 ~ 2016.03.10";
    cell.director.text = @"阿加莎推理巨作——爱乐汇·拉得";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _jumpToDetail();
}

- (void)createTableView{
    
    UILabel *searchResult = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 76.5) / 2, 77, 76.5, 18.5)];
    searchResult.text = @"- 搜索结果 -";
    searchResult.font = [UIFont fontWithName:@"PingFang SC" size:13];
    searchResult.textColor = [UIColor whiteColor];
    [self addSubview:searchResult];
    
    _searchTableView= [[UITableView alloc] initWithFrame:CGRectMake(0,107.5, SCREEN_WIDTH, SCREEN_HEIGHT - 107.5) style:UITableViewStylePlain];
    _searchTableView.delegate = self;
    _searchTableView.dataSource = self;
    _searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _searchTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
    _searchTableView.backgroundColor = [UIColor clearColor];
    [self addSubview:_searchTableView];
    
    [_searchTableView registerNib:[UINib nibWithNibName:@"OnlineTableViewCell" bundle:nil] forCellReuseIdentifier:OnlineTableViewCellID];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_searchTextField resignFirstResponder];
}

@end
